//options
clear all
set maxvar  32000, permanently
set matsize 11000, permanently
set more off, permanently
global bootstraps 1000
set seed 23

// macros
global klmshare				: env klmshare
global projects				: env projects
global klmperry             : env klmperry
global storageb				: env storageb

global perrydatas       = "${klmperry}/CBA/data/perry/raw"
global perrydata        = "${klmperry}/CBA/data/perry/clean"
global masterinter  	= "${klmshare}/CurrentRAs/FredB/test"
global output           = "${projects}/ece_parenting/tex_files/other"
global dataperry        = "$klmperry/PerryPreschool/Data/Perry_PARI_and_Other_Data/PARI/DATA_PARI/"
global abcjpeanalysis   = "${klmshare}/Data_Central/Abecedarian/data/ABC-CARE/extensions/cba-iv/"
global dataihdp         = "${projects}/ece_parenting/data"
global dataihdp         = "$storageb/dc_data/"

cd  $dataihdp
use ihdp_data, clear
rename _all, lower

replace   bw = bw/1000

// impute iq
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.
foreach var of varlist stndscor iqcage { 
	replace `var' = mdi_24cor    if stndscor ==. & iqcage ==.
}
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.

// low birthweight
gen lb = 1
rename tot_siblings_natural siblings_natural

// list relevant variables
global baseline_child      twin sex bw lb anga black hispanic 
global baseline_mother     mage meduc works married 
global baseline_household  welfare siblings_natural employed_adult
global baseline_economy    employment medinc gpc
global outputs             iqcage stndscor
global inputs              cum_avg_daycare_36m_sum

// mark baseline sample
reg  $baseline_child $baseline_mother $baseline_household $baseline_economy $outputs $inputs
gen  sample1 = e(sample) 
keep if sample1 == 1

// parenting latents, ages 1 and 3
foreach var of varlist alt_subscale_1_12-alt_subscale_6_12 alt_subscale_1_36-alt_subscale_8_36 {
	summ `var'
	gen  `var'_std = (`var' - r(mean))/r(sd)
}
# delimit
sem (_cons@0 X -> alt_subscale_1_12_std) (_cons@0 X -> alt_subscale_2_12_std) (_cons@0 X -> alt_subscale_3_12_std) 
	(_cons@0 X -> alt_subscale_4_12_std) (_cons@0 X -> alt_subscale_5_12_std) (_cons@0 X -> alt_subscale_6_12_std), method(adf);
predict parenting_age1, latent;
sem (_cons@0 X -> alt_subscale_1_36_std) (_cons@0 X -> alt_subscale_2_36_std) (_cons@0 X -> alt_subscale_3_36_std) (_cons@0 X -> alt_subscale_4_36_std)
	(_cons@0 X -> alt_subscale_5_36_std) (_cons@0 X -> alt_subscale_6_36_std) (_cons@0 X -> alt_subscale_7_36_std) (_cons@0 X -> alt_subscale_8_36_std), 
																																						 cov(e.alt_subscale_1_36_std*e.alt_subscale_2_36_std)
																																						 cov(e.alt_subscale_3_36_std*e.alt_subscale_4_36_std)
																																						 cov(e.alt_subscale_5_36_std*e.alt_subscale_6_36_std)
																																						 cov(e.alt_subscale_7_36_std*e.alt_subscale_8_36_std)
																																						 cov(e.alt_subscale_4_36_std*e.alt_subscale_7_36_std)
																																						 cov(e.alt_subscale_2_36_std*e.alt_subscale_7_36_std) method(adf);		
 predict parenting_age3, latent;
# delimit cr
egen    parenting_ages13 = rowmean(parenting_age1 parenting_age3)

// standardize latents in sample
foreach var of varlist parenting_ages13 {
	summ    `var' if tg == 0
	gen     `var'_std = (`var' - r(mean))/r(sd)
	replace `var'_std = `var'_std + 100
}
// log and standardize childcare variable 
summ    cum_avg_daycare_36m_sum  if tg == 0 
replace cum_avg_daycare_36m_sum  = (cum_avg_daycare_36m_sum    - r(mean))/r(sd)
replace cum_avg_daycare_36m_sum  =  cum_avg_daycare_36m_sum    + r(mean)

rename cum_avg_daycare_36m_sum c
rename parenting_ages13_std    p

global inputs_std c p
keep   ihdp site tg bwg $baseline_child $baseline_mother $baseline_household $baseline_economy $inputs_std $outputs

// describe by sample
global sample_all          if twin != .
global sample_singletons   if twin == 0
global sample_twins        if twin == 1

// outside parameters (references in paper)
matrix twin_us     = [.02]
matrix sex_us      = [.51]
matrix bw_us       = [3.35044]
matrix lb_us       = [.06]
matrix anga_us     = [41.62]
matrix black_us    = [.16]
matrix hispanic_us = [.16] 
matrix mage_us     = [25.82]
matrix meduc_us    = [12.59]
matrix works_us    = [.54]
matrix married_us  = [.78]
matrix welfare_us 	     	   = [.03]
matrix siblings_natural_us     = [.85]
matrix employed_adult_us       = [1.18]
matrix employment_us = [92.80]
matrix medinc_us     = [54.33]
matrix gpc_us        = [7972.09]

// describe
matrix all = J(1,10,.)
foreach var of varlist $baseline_child $baseline_mother $baseline_household {
	foreach group in all singletons twins {
		bootstrap, strata(bwg site tg) reps($bootstraps) : reg `var' tg ${sample_`group'}
		matrix  c`var'_g`group' = e(b)[1,2]
		matrix  b`var'_g`group' = e(b)[1,1]
		matrix se`var'_g`group'      = sqrt(e(V)[1,1])
		matrix  t`var'_g`group'      = abs(b`var'_g`group'[1,1]/se`var'_g`group'[1,1])
		matrix df`var'_g`group' = e(N) - e(rank)
		matrix  p`var'_g`group' = 2*(1 - normal(t`var'_g`group'[1,1]))
		
		matrix `var'_`group' = [c`var'_g`group',b`var'_g`group',p`var'_g`group'] 
	}
	matrix `var' = [`var'_singletons,`var'_twins,`var'_all,`var'_us]
	matrix all = [all \ `var'] 
}
foreach var of varlist $baseline_economy {
	foreach group in all singletons twins {
		bootstrap, strata(bwg tg) reps($bootstraps) : reg `var' tg ${sample_`group'}
		matrix  c`var'_g`group' = e(b)[1,2]
		matrix  b`var'_g`group' = e(b)[1,1]
		matrix se`var'_g`group'      = sqrt(e(V)[1,1])
		matrix  t`var'_g`group'      = abs(b`var'_g`group'[1,1]/se`var'_g`group'[1,1])
		matrix df`var'_g`group' = e(N) - e(rank)
		matrix  p`var'_g`group' = 2*(1 - normal(t`var'_g`group'[1,1]))
		
		matrix `var'_`group' = [c`var'_g`group',b`var'_g`group',p`var'_g`group'] 
	}
	matrix `var' = [`var'_singletons,`var'_twins,`var'_all,`var'_us]
	matrix all = [all \ `var'] 
}
matrix all = all[2...,1...]
// test
foreach group in all singletons twins {
	reg tg $baseline_child $baseline_mother $baseline_household ${sample_`group'}, robust
	test $baseline_child $baseline_mother $baseline_household
	matrix `group'_tests = [r(F),r(p),e(N)]
}


matrix all = [ J(6,10,.)  \ all[1..7,1...] \ J(1,10,.) \ all[8..11,1...] \ J(1,10,.) \ all[12..14,1...] \ J(1,10,.) \ all[15...,1...] \ J(2,10,.) \ [singletons_tests,twins_tests,all_tests,.]]
clear
svmat all
gen   n = _n

// format
foreach var of varlist all* {
	format  `var' %15.2fc
	replace `var' = round(`var',.001) 
	gen    `var'and = "&" if n !=1
}

// header
gen all0and = "&" if n != 1
gen       all1_0 = "\begin{tabular}{ l cccccccccc} \toprule" if n == 1

replace all1_0   = " "                    	      		     if n == 2
replace all0and  = " & \multicolumn{1}{c}{(1)}"     if n == 2
replace all1and  = " & \multicolumn{1}{c}{(2)}"     if n == 2
replace all2and  = " & \multicolumn{1}{c}{(3)}"     if n == 2
replace all3and  = " & \multicolumn{1}{c}{(4)}"     if n == 2
replace all4and  = " & \multicolumn{1}{c}{(5)}"     if n == 2
replace all5and  = " & \multicolumn{1}{c}{(6)}"     if n == 2
replace all6and  = " & \multicolumn{1}{c}{(7)}"     if n == 2
replace all7and  = " & \multicolumn{1}{c}{(8)}"     if n == 2
replace all8and  = " & \multicolumn{1}{c}{(9)}"     if n == 2
replace all9and  = " & \multicolumn{1}{c}{(10)}"    if n == 2

replace all1_0   = " & "                    	      		    			       if n == 3
replace all0and  = "  \multicolumn{9}{c}{Infant Health and Development Program}"   if n == 3
replace all1and  = "   "           						      		               if n == 3
replace all2and  = "   "           						      		               if n == 3
replace all3and  = "   "           						      		               if n == 3
replace all4and  = "   "           						      		               if n == 3
replace all5and  = "   "           						      		               if n == 3
replace all6and  = "   "           						      		               if n == 3
replace all7and  = "   "           						      		               if n == 3
replace all8and  = "   "           						      		               if n == 3
replace all9and  = "   "           						      		               if n == 3

replace all1_0   = " "                    	      		 if n == 4
replace all0and  = " & \multicolumn{3}{c}{Singletons}"   if n == 4
replace all1and  = "   "           						 if n == 4
replace all2and  = "   "           						 if n == 4
replace all3and  = " & \multicolumn{3}{c}{Twins}"        if n == 4
replace all4and  = "   "           						 if n == 4
replace all5and  = "   "           						 if n == 4
replace all6and  = " & \multicolumn{3}{c}{All}"          if n == 4
replace all7and  = "   "           						 if n == 4
replace all8and  = "   "           						 if n == 4
replace all9and  = "   "           						 if n == 4

replace all1_0   = " "                    	      		  if n == 5
replace all0and  = " & \multicolumn{1}{c}{Control}"       if n == 5
replace all1and  = " & \multicolumn{1}{c}{$ \Delta $ }"   if n == 5
replace all2and  = " & \multicolumn{1}{c}{$ p$-value }"   if n == 5
replace all3and  = " & \multicolumn{1}{c}{Control}"       if n == 5
replace all4and  = " & \multicolumn{1}{c}{$ \Delta $ }"   if n == 5
replace all5and  = " & \multicolumn{1}{c}{$ p$-value }"   if n == 5
replace all6and  = " & \multicolumn{1}{c}{Control}"       if n == 5
replace all7and  = " & \multicolumn{1}{c}{$ \Delta $ }"   if n == 5
replace all8and  = " & \multicolumn{1}{c}{$ p$-value }"   if n == 5
replace all9and  = " & \multicolumn{1}{c}{US in 1985}"    if n == 5

replace all1_0   = "  \multicolumn{11}{l}{\textbf{\textit{Panel a}. Children}}"            						 if n == 6
replace all0and  = "   "           						      		                		   					 if n == 6
replace all1and  = "   "           						      		                		   					 if n == 6
replace all2and  = "   "           						      		                		   					 if n == 6
replace all3and  = "   "           						      		               			   					 if n == 6
replace all4and  = "   "           						      		               			   					 if n == 6
replace all5and  = "   "           						      		               			   					 if n == 6
replace all6and  = "   "           						      		                		   					 if n == 6
replace all7and  = "   "           						      		               			   					 if n == 6
replace all8and  = "   "           						      		               			   					 if n == 6
replace all9and  = "   "           						      		               			   					 if n == 6

replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Twin}" 		 		               if n == 7  
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Male}" 		 		               if n == 8 
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Birth Weight (kilograms)}" 	       if n == 9 
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Low Birth Weight}" 		 		   if n == 10 
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Gestational Age (week)}"             if n == 11 
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Black}"    		 		           if n == 12 
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Hispanic}"         		           if n == 13

replace all1_0   = "  \multicolumn{11}{l}{\textbf{\textit{Panel b}. Mother}}"            						 if n == 14
replace all0and  = "   "           						      		                		   					 if n == 14
replace all1and  = "   "           						      		                		   					 if n == 14
replace all2and  = "   "           						      		                		   					 if n == 14
replace all3and  = "   "           						      		               			   					 if n == 14
replace all4and  = "   "           						      		               			   					 if n == 14
replace all5and  = "   "           						      		               			   					 if n == 14
replace all6and  = "   "           						      		                		   					 if n == 14
replace all7and  = "   "           						      		               			   					 if n == 14
replace all8and  = "   "           						      		               			   					 if n == 14
replace all9and  = "   "           						      		               			   					 if n == 14

replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Age}" 		               if n == 15  
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Education}" 	               if n == 16
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Works}" 	                   if n == 17
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Married}" 		 		   if n == 18 

replace all1_0   = "  \multicolumn{11}{l}{\textbf{\textit{Panel c}. Household}}"          						 if n == 19
replace all0and  = "   "           						      		                		   					 if n == 19
replace all1and  = "   "           						      		                		   					 if n == 19
replace all2and  = "   "           						      		                		   					 if n == 19
replace all3and  = "   "           						      		               			   					 if n == 19
replace all4and  = "   "           						      		               			   					 if n == 19
replace all5and  = "   "           						      		               			   					 if n == 19
replace all6and  = "   "           						      		                		   					 if n == 19
replace all7and  = "   "           						      		               			   					 if n == 19
replace all8and  = "   "           						      		               			   					 if n == 19
replace all9and  = "   "           						      		               			   					 if n == 19

replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Uses Social Programs}"       if n == 20  
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Siblings}" 	               if n == 21
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Employed Adults}" 	       if n == 22
 
replace all1_0   = "  \multicolumn{11}{l}{\textbf{\textit{Panel d}. Economy}}"          						 if n == 23
replace all0and  = "   "           						      		                		   					 if n == 23
replace all1and  = "   "           						      		                		   					 if n == 23
replace all2and  = "   "           						      		                		   					 if n == 23
replace all3and  = "   "           						      		               			   					 if n == 23
replace all4and  = "   "           						      		               			   					 if n == 23
replace all5and  = "   "           						      		               			   					 if n == 23
replace all6and  = "   "           						      		                		   					 if n == 23
replace all7and  = "   "           						      		               			   					 if n == 23
replace all8and  = "   "           						      		               			   					 if n == 23
replace all9and  = "   "           						      		               			   					 if n == 23

replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} \% Employed}"                                                if n == 24  
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} Median Income (1,000s)}" 	                                   if n == 25
replace all1_0  = "   \multicolumn{1}{l}{\hspace{2mm} \footnotesize Government Expenditure per Capita }" 	       if n == 26
 
replace all1_0   = "  \multicolumn{11}{l}{\textbf{\textit{Panel e}. Joint Tests}}"        						 if n == 27
replace all0and  = "   "           						      		                		   					 if n == 27
replace all1and  = "   "           						      		                		   					 if n == 27
replace all2and  = "   "           						      		                		   					 if n == 27
replace all3and  = "   "           						      		               			   					 if n == 27
replace all4and  = "   "           						      		               			   					 if n == 27
replace all5and  = "   "           						      		               			   					 if n == 27
replace all6and  = "   "           						      		                		   					 if n == 27
replace all7and  = "   "           						      		               			   					 if n == 27
replace all8and  = "   "           						      		               			   					 if n == 27
replace all9and  = "   "           						      		               			   					 if n == 27

replace all1_0   = " "                    	      		    if n == 28
replace all0and  = " & \multicolumn{1}{c}{$ F$-statistic}"  if n == 28
replace all1and  = " & \multicolumn{1}{c}{$ p$-value}"      if n == 28
replace all2and  = " & \multicolumn{1}{c}{$ N$ }"           if n == 28

replace all3and  = " & \multicolumn{1}{c}{$ F$-statistic}"  if n == 28
replace all4and  = " & \multicolumn{1}{c}{$ p$-value}"      if n == 28
replace all5and  = " & \multicolumn{1}{c}{$ N$ }"           if n == 28

replace all6and  = " & \multicolumn{1}{c}{$ F$-statistic}"  if n == 28
replace all7and  = " & \multicolumn{1}{c}{$ p$-value}"      if n == 28
replace all8and  = " & \multicolumn{1}{c}{$ N$ }"           if n == 28


// order
global orderlist all1_0 all0and
foreach num of numlist 1(1)10 {
	global orderlist $orderlist all`num' all`num'and
}
order $orderlist

// other formating
replace all10and = "\\   "
replace all10and = " \\ \midrule " 					       if n == 2
replace all10and = " \\ \cmidrule(l{.15cm}r{.15cm}){2-10}" if n == 3
replace all10and = " \\ \cmidrule(l{.15cm}r{.15cm}){2-4} \cmidrule(l{.15cm}r{.15cm}){5-7} \cmidrule(l{.15cm}r{.15cm}){8-10}" if n == 4 | n == 28
replace all10and = " \\ \midrule " 		                   if n == 5  | n == 13 | n == 18 | n == 22 | n == 26
replace all10and = " \\ \bottomrule \end{tabular}"         if n == 29
replace all10and = " " 								       if n == 1

// put together in string
gen all = " "
foreach num of numlist 1(1)10 {
	gen all`num'b = all`num'
	tostring all`num'b , replace force format(%15.0fc)
	tostring all`num'  , replace force format(%15.2fc)
	replace  all`num'  = "" if all`num'  == "."
	replace  all`num'  = "1.00" if (n == 7 | n == 10) & (`num' == 3 | `num' == 6 | `num' == 9)
	replace  all`num'  = all`num'b         if n == 29 & (`num' == 3 | `num' == 6 | `num' == 9) 
}

drop *b
global orderin all1_0 all0and
foreach num of numlist 1(1)10 {
	global orderin $orderin all`num' all`num'and 
}
global orderin $orderin all`num'
order $orderin n

foreach var of varlist all1_0-all10and {	
	replace  all = all + `var'
	replace  all`num' = " " if all`num' == "."
}

// save in tex format
keep all
cd $output
outsheet using baselinedescribe.tex, noquote nonames replace
